package org.mclll.xwangplugin.questTracking.questGoal;

import me.pikamug.quests.events.quester.BukkitQuesterPostUpdateObjectiveEvent;
import me.pikamug.quests.quests.components.BukkitObjective;
import me.pikamug.quests.quests.components.Stage;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scoreboard.Objective;
import org.jetbrains.annotations.NotNull;
import org.mclll.xwangplugin.log.GlobalLog;
import org.mclll.xwangplugin.log.LogUtil;
import org.mclll.xwangplugin.log.LoggerFactory;
import org.mclll.xwangplugin.questTracking.persistence.Persistence;
import org.mclll.xwangplugin.util.GetChineseNameUtil;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: geduo
 * @date: 2023/10/12 22:14
 */
public class BreakBlockQuestTrack {
    private static final LogUtil log = LoggerFactory.get(GlobalLog.class);
    private static final String FORMAT_PREFIX = "采集 %s";
    public static final String LINE_FORMAT = ChatColor.YELLOW + FORMAT_PREFIX + " (%d/%d)";

    public static void questTracking(Objective context, Integer line, Stage questsStage) {
        LinkedList<ItemStack> toBreakList = (LinkedList<ItemStack>) questsStage.getBlocksToBreak();
        if (toBreakList.isEmpty()) {
            return;
        }
        for (ItemStack itemStack : toBreakList) {
            context.getScore(getGoalFormat(itemStack, 0, itemStack.getAmount()))
                    .setScore(line--);
        }
    }

    public static String getGoalFormat(ItemStack itemStack, Integer progress, Integer total) {
        String name = GetChineseNameUtil.get(itemStack.getType());
        return String.format(LINE_FORMAT, name, progress, total);
    }

    @NotNull
    public static String getFinishFormat(ItemStack breakBlock) {
        String chineseName = GetChineseNameUtil.get(breakBlock.getType());
        return ChatColor.GRAY + String.format(FORMAT_PREFIX, chineseName) + " (✓)";
    }

    public static void goalFinish(BukkitQuesterPostUpdateObjectiveEvent event, List<Persistence.SerializeScore> scoreboard) {
        Map<String, Persistence.SerializeScore> collect = scoreboard.stream().collect(Collectors.toMap(item -> item.getName().split(" \\(")[0].substring(5), score -> score));
        BukkitObjective questsObjective = (BukkitObjective) event.getObjective();
        ItemStack breakBlock = (ItemStack) questsObjective.getGoalObject();
        String chineseName = GetChineseNameUtil.get(breakBlock.getType());
        Persistence.SerializeScore score = collect.get(chineseName);
        if (score == null) {
            return;
        }
        int amount = questsObjective.getGoal() - questsObjective.getProgress();
        if (amount == 0) {
            score.setName(getFinishFormat(breakBlock));
            score.setValue(scoreboard.get(scoreboard.size() - 1).getValue() - 1);
        } else {
            // 0说明是放置的任务，破坏了方块
            if (score.getName().startsWith(LINE_FORMAT.substring(0, 4))) {
                score.setName(getGoalFormat(breakBlock, questsObjective.getProgress(), questsObjective.getGoal()));
            } else {
                score.setName(PlaceBlockQuestTrack.getGoalFormat(breakBlock, questsObjective.getProgress(), questsObjective.getGoal()));
            }
        }
    }

}
